
local LoginPanel = class("LoginPanel", BasePanel)

local AnimSlider = class("AnimSlider")
function AnimSlider:ctor(slider, dataDelegate)
    self.slider = slider
    self.preSequence = nil
    self.sequence = nil
    self.postSequence = nil
    self.dataDelegate = dataDelegate

    self.oneBarDruation = 0.3
    self.interval = 0.5
    self.timescale = 1
end

function AnimSlider:IsPlaying()
    if self.preSequence or self.sequence or self.postSequence then
        return true
    end
    return false
end

function AnimSlider:CallStepOver()
    local callback = self.dataDelegate.callback
    if callback then
        callback()
    end
end

function AnimSlider:CreatePreSequence(onComplete)
    local slider = self.slider
    local getCountFunc = self.dataDelegate.getCountFunc
    if getCountFunc() < 1 then
        if onComplete then
            onComplete()
        end
        return
    end

    local preSequence = CS.DG.Tweening.DOTween.Sequence()
    preSequence:SetAutoKill(true)
    preSequence:Append(slider:DOValue(1.0, self.oneBarDruation))
    preSequence:AppendCallback(function()
        self:CallStepOver()
    end)
    preSequence:AppendInterval(self.interval)
    preSequence:OnComplete(function()
        slider.value = 0
        if onComplete then
            onComplete()
        end
        self.preSequence = nil
    end)
    preSequence.timeScale = self.timescale
    self.preSequence = preSequence
end

function AnimSlider:CreateSequence(onComplete)
    local slider = self.slider
    local getCountFunc = self.dataDelegate.getCountFunc
    if getCountFunc() < 1 then
        if onComplete then
            onComplete()
        end
        return
    end

    local sequence = CS.DG.Tweening.DOTween.Sequence()
    sequence:SetAutoKill(true)
    sequence:Append(slider:DOValue(1.0, self.oneBarDruation))
    sequence:AppendCallback(function()
        self:CallStepOver()
    end)
    sequence:AppendInterval(self.interval)
    sequence:AppendCallback(function()
        if getCountFunc() >= 1 then
            sequence:Restart()
            self.timescale = getCountFunc() > 4 and 4 or 1
            sequence.timeScale = self.timescale
        end
    end)
    sequence:OnComplete(function()
        slider.value = 0
        if onComplete then
            onComplete()
        end
        self.sequence = nil
    end)
    sequence.timeScale = self.timescale
    self.sequence = sequence
end

function AnimSlider:CreatePostSequence(onComplete)
    local slider = self.slider
    local getCountFunc = self.dataDelegate.getCountFunc
    
    self.goal = getCountFunc()
    local postSequence = CS.DG.Tweening.DOTween.Sequence()
    postSequence:SetAutoKill(true)
    postSequence:Append(slider:DOValue(getCountFunc(), self.oneBarDruation*getCountFunc()))
    postSequence:OnComplete(function()
        self.postSequence = nil
        if self.goal == getCountFunc() then
            if onComplete then
                onComplete()
            end
        else
            self:Restart()
        end
    end)
    postSequence.timeScale = self.timescale
    self.postSequence = postSequence
end

function AnimSlider:Play()
    local getCountFunc = self.dataDelegate.getCountFunc
    print("play "..getCountFunc())

    self.timescale = getCountFunc() > 4 and 4 or 1


    self:CreatePreSequence(function()
        self:CreateSequence(function()
            self:CreatePostSequence(function()
                self.dataDelegate.onComplete()
            end)
        end)
    end)
end

function AnimSlider:Restart()
    self:Play()
end


--------------------------------------------------------------------

function LoginPanel:ctor(goParent, res, args)
    LoginPanel.super.ctor(self, goParent, res, args)    
end

function LoginPanel:AddFloatText(str)
    local prefab = GameMgr.instance.resMgr:LoadAsset("Prefabs/goFloatText")

    local canvas = CS.UnityEngine.GameObject.FindGameObjectWithTag("Canvas")
    self.canvas = canvas

    local textObj = util.newObject(prefab, canvas.transform)
    textObj.name = "floatText"

    local lbContent = util.pickC(textObj,"lbContent","Text")
    lbContent.text = str

    local sequence = CS.DG.Tweening.DOTween.Sequence()
    sequence:SetAutoKill(true)
    sequence:Append(textObj.transform:DOLocalMove(Vector3(0, 200, 0), 0.5))
    sequence:OnComplete(function()
        GameObject.Destroy(textObj)
    end)
end

function LoginPanel:OnLevelUp()
    self:AddFloatText("LV UPPPPPP")
    self.level = math.floor(self.level) + 1

    local lbLevel = self.lbLevel
    lbLevel.text = self.level
end

function LoginPanel:start()

    local go = self.go

    local btnBkg = util.pickC(go, "imgBkg", "Button")
    btnBkg.onClick:AddListener(function()
        --Navigator.enterFrame(AllFrames.CardPlayFrame)
        local animSlider = self.animSlider
        if animSlider:IsPlaying() then
            print("add")
            self.levelTo = self.levelTo + 0.3
        else
            print("play")
            self.levelTo = self.levelTo + 0.3
        end
    end)

    local imgHeart = util.pickC(go, "imgHeart", "Image")
    local tweener = imgHeart.transform:DOScale(1.3, 0.5)
    tweener:SetLoops(-1, CS.DG.Tweening.LoopType.Yoyo)

    local lbLevel = util.pickC(go, "lbLevel", "Text")
    self.lbLevel = lbLevel

    local sliderExp = util.pickC(go, "sliderExp", "Slider")
    self.sliderExp = sliderExp

    self.level = 0.3
    self.levelTo = 0.8
    local animSlider = AnimSlider.new(sliderExp, {
        callback = function()
            self:OnLevelUp()
        end,
        onComplete = function()
            self.level = self.levelTo
        end,
        getCountFunc = function()
            return self.levelTo - math.floor(self.level)
        end
    })
    sliderExp.value = self.level
    self.animSlider = animSlider

    local goHeart1 = util.pick(go, "goHeart1/imgHL")
    local goHeart2 = util.pick(go, "goHeart2/imgHL")
    local goHeart3 = util.pick(go, "goHeart3/imgHL")

    self:StartCoroutine(function()
        while true do
            coroutine.yield( CS.UnityEngine.WaitForEndOfFrame() )
    
            if animSlider.preSequence then
                goHeart1:SetActive(true)
            else
                goHeart1:SetActive(false)
            end
            if animSlider.sequence then
                goHeart2:SetActive(true)
            else
                goHeart2:SetActive(false)
            end
            if animSlider.postSequence then
                goHeart3:SetActive(true)
            else
                goHeart3:SetActive(false)
            end
        end
    end)
    
    self:StartCoroutine(function()
        while true do
            coroutine.yield( CS.UnityEngine.WaitForSeconds(1.0) )

            self.levelTo = self.levelTo + math.random( 5, 80 )/10
            if animSlider:IsPlaying() then
            else

                animSlider:Play()
            end
        end
    end)
end


return LoginPanel